package com.blog.wl2023.service.logged;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.blog.wl2023.entity.ArticleComment;
import com.blog.wl2023.entity.TokenInfo;
import com.blog.wl2023.entity.User;
import com.blog.wl2023.entity.response.Result;
import com.blog.wl2023.repository.ArticleCommentRepository;
import com.blog.wl2023.repository.ArticleRepository;
import com.blog.wl2023.repository.UserRepository;
import com.blog.wl2023.utils.CommUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

import static cn.hutool.extra.mail.MailUtil.send;

/**
 * @author wule
 * @date 2023-09-28  21:59
 * @desc 登录之后接口的服务类, 主要是逻辑操作
 */

@Service
public class LoggedService {

	@Autowired
	ArticleCommentRepository articleCommentRepository;

	@Autowired
	ArticleRepository articleRepository;


	@Autowired
	UserRepository userRepository;

	public Result postComment(ArticleComment articleComment) {
		//生成的UUID是带-的字符串
		String uuid = IdUtil.randomUUID();
		articleComment.setUuid(uuid);
		articleComment.setIsDelete(0);

		// 关联User对象到ArticleComment
		articleComment.setUser(new User(articleComment.getUserId()));

		articleCommentRepository.save(articleComment);
		//在文章信息中给文章的总评论数加一
		articleRepository.updateArticleCommentSizeByUUid(articleComment.getArticleId());
		return Result.ok();
	}

	public Result modifyEmailAddressGetCode(String newEmail, String oldEmail) {
		//判断新的邮箱是否已经注册
		User userByNewEmail1 = userRepository.findUserByEmail(newEmail);
		if (!BeanUtil.isEmpty(userByNewEmail1)) {
			//存在
			return Result.fail(201, "该账号已注册,不能作为新邮箱!");
		}
		//生成并且返回验证码,数据库插入code
		String code = CommUtil.generateRandomCode(6);
		//将验证码存入数据库(考虑获取验证码之后核对新邮箱是否正确)
		userRepository.updateCode(oldEmail, code);
		//发送验证码到新邮箱
		send(newEmail, "修改邮箱", "<h1>验证码:" + code + "</h1></br>感谢您使用PRIEST网站。如非您本人操作，请忽略此邮件。", true);
		//发送警告到老邮箱
		send(oldEmail, "邮箱修改提示", "尊敬的用户，<br>" + "我们通知您，您的邮箱正在修改为:<h1>" + newEmail + "</h1>。如果您未执行此操作，请立即联系我们的客服团队,并修改密码。<br>" + "------PRIEST团队", true);
		return Result.ok();
	}

	public Result modifyEmailAddress(String newEmail, String oldEmail, String code) {
		User userByOldEmail = userRepository.findUserByEmail(oldEmail);
		//对比验证码
		if (userByOldEmail.getCode().equals(code)) {
			//判断新的邮箱是否已经注册
			User userByNewEmail1 = userRepository.findUserByEmail(newEmail);
			if (!BeanUtil.isEmpty(userByNewEmail1)) {
				//存在
				return Result.fail(201, "该账号已注册,不能作为新邮箱!");
			}
			//对比成功:修改邮箱,然后清空验证码
			userByOldEmail.setEmail(newEmail);
			userByOldEmail.setCode("");
			userRepository.save(userByOldEmail);
			return Result.ok("修改成功!");
		} else {
			//对比失败返回操作失败
			return Result.fail(201, "验证码错误,请输入正确的验证码!");
		}
	}

	public Result updateUserInfo(User user) {
		userRepository.updateUserInfo(user.getUuid(), user.getUsername(), user.getGender(), user.getIntroduction());
		//获取用户信息
		User newUser = userRepository.findUserByUUid(user.getUuid());
		SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
		newUser.setTokenInfo(new TokenInfo(tokenInfo.getTokenName(), tokenInfo.getTokenValue()));
		return Result.ok(newUser);
	}
}
